const api = require('../../../utils/api') // 引入API
const {
  myRequest
} = require('../../../utils/myRequest') // 请求函数
Page({

  data: {
    detailData: {},
    infoList: [], // 商品详情列表
    issueList: [], // 问答
    gallery: [], // 轮播
    attribute: [], // 活动
    comment: {}, // 评论
    brand: {}, // 品牌
    specificationList: [], // 规格
    productList: [], // 产品列表
    addressId: '' // 地址id
  },
  onLoad: function (options) {
    // 从本地获取id
    wx.getStorage({
      key: 'addressId',
      success: (res)=>{
        let addressId = res.data;
        this.setData({
          addressId: addressId
        })
      },
    });
    let detailData = JSON.parse(options.detailData)
    this.setData({
      detailData: detailData,
      id: detailData.id
    })
    myRequest({
      url: api.goodsDetail,
      data: {
        id: detailData.id
      },
      success: res => {
        let infoList = res.data.data.info;
        let issueList = res.data.data.issue;
        let attribute = res.data.data.attribute;
        let gallery = res.data.data.gallery;
        let comment = res.data.data.comment;
        let brand = res.data.data.brand;
        let specificationList = res.data.data.specificationList;
        let productList = res.data.data.productList;
        this.setData({
          infoList: infoList,
          issueList: issueList,
          attribute: attribute,
          gallery: gallery,
          comment: comment,
          brand: brand,
          specificationList: specificationList,
          productList: productList
        })
      }
    })
    // 下单前信息确认
    myRequest({
      url: api.orderSubmit,
      method: "POST",
      data: {
        addressId: this.data.addressId,
        couponId: 0
      },
      success: res => {
        console.log(res);
        
      }
    })
  },

  // 跳转到订单详情
  toOrderDetail() {
    let { detailData  } = this.data;
    let OrderdetailData = {
        goodsTotalCount: detailData.goodsTotalCount,
        picurlId: detailData.colorId,
        colorId: detailData.colorId,
        sizeId: detailData.sizeId,
        totalPrice: detailData.totalPrice,
        id: detailData.id
    }
    OrderdetailData = JSON.stringify(OrderdetailData) // 需要解析
    wx.navigateTo({
      url: '../orderDetail/orderDetail?OrderdetailData=' + OrderdetailData,
    });
  },
  // // 购买数量减少函数
  // cartDecerase() {
  //   let [price, num] = [0, 0]
  //   const {
  //     goodsTotalCount,
  //     productListArr
  //   } = this.data;
  //   num = goodsTotalCount - 1; // 数量减一
  //   // 数量小于等于0,不在减少
  //   if (num <= 0) {
  //     return;
  //   }
  //   price = num * productListArr.retail_price // 计算总价
  //   this.setData({
  //     goodsTotalCount: num,
  //     totalPrice: price
  //   })
  // },
  // // 购买数量增加函数
  // cartAdd() {
  //   let [price, num] = [0, 0]
  //   const {
  //     goodsTotalCount,
  //     productListArr
  //   } = this.data;
  //   num = goodsTotalCount + 1; // 数量加一
  //   // 计算总价
  //   price += num * productListArr.retail_price
  //   this.setData({
  //     goodsTotalCount: num,
  //     totalPrice: price
  //   })
  // },
  onReachBottom: function () {

  },
})